home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / src / hpux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-19  |  7.7 KB  |  347 lines

  1. /* @(#) $Header: hpux.c,v 1.20 91/09/17 22:21:38 deyke Exp $ */
  2.  
  3. #include <sys/types.h>
  4.  
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <sys/wait.h>
  11. #include <termio.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14.  
  15. #include "global.h"
  16. #include "iface.h"
  17. #include "timer.h"
  18. #include "files.h"
  19. #include "hardware.h"
  20. #include "login.h"
  21. #include "commands.h"
  22. #include "main.h"
  23. #include "hpux.h"
  24.  
  25. extern long  sigsetmask();
  26.  
  27. #define TIMEOUT 120
  28.  
  29. static int  chkread[2];
  30. static int  actread[2];
  31. static void (*readfnc[_NFILE]) __ARGS((void *));
  32. static void *readarg[_NFILE];
  33.  
  34. static int  chkwrite[2];
  35. static int  actwrite[2];
  36. static void (*writefnc[_NFILE]) __ARGS((void *));
  37. static void *writearg[_NFILE];
  38.  
  39. static int  chkexcp[2];
  40. static int  actexcp[2];
  41. static void (*excpfnc[_NFILE]) __ARGS((void *));
  42. static void *excparg[_NFILE];
  43.  
  44. static int  nfds = -1;
  45.  
  46. static int  local_kbd;
  47.  
  48. static struct termio curr_termio;
  49. static struct termio prev_termio;
  50.  
  51. static void check_files_changed __ARGS((void));
  52.  
  53. /*---------------------------------------------------------------------------*/
  54.  
  55. static void sigpipe_handler(sig, code, scp)
  56. int  sig;
  57. int  code;
  58. struct sigcontext *scp;
  59. {
  60.   scp->sc_syscall_action = SIG_RETURN;
  61. }
  62.  
  63. /*---------------------------------------------------------------------------*/
  64.  
  65. void ioinit()
  66. {
  67.  
  68.   int i;
  69.   struct sigvec vec;
  70.  
  71. #define fixdir(name, mode) \
  72.     { mkdir((name), (mode)); chmod((name), (mode)); }
  73.  
  74.   fixdir("/tcp", 0755);
  75.   fixdir("/tcp/sockets", 0755);
  76.   fixdir("/tcp/.sockets", 0700);
  77.   fixdir("/tcp/logs", 0700);
  78.  
  79.   if (local_kbd = isatty(0)) {
  80.     ioctl(0, TCGETA, &prev_termio);
  81.     curr_termio = prev_termio;
  82.     curr_termio.c_iflag = BRKINT | ICRNL | IXON | IXANY | IXOFF;
  83.     curr_termio.c_lflag = 0;
  84.     curr_termio.c_cc[VMIN] = 0;
  85.     curr_termio.c_cc[VTIME] = 0;
  86.     ioctl(0, TCSETA, &curr_termio);
  87.     printf("\033&s1A");   /* enable XmitFnctn */
  88.     on_read(0, keyboard, (void *) 0);
  89.   } else {
  90.     for (i = 0; i < _NFILE; i++) close(i);
  91.     setpgrp();
  92.     fopen("/dev/null", "r+");
  93.     fopen("/dev/null", "r+");
  94.     fopen("/dev/null", "r+");
  95.     remote_kbd_initialize();
  96.   }
  97.   vec.sv_mask = vec.sv_flags = 0;
  98.   vec.sv_handler = sigpipe_handler;
  99.   sigvector(SIGPIPE, &vec, (struct sigvec *) 0);
  100.   vec.sv_handler = (void (*)()) abort;
  101.   sigvector(SIGALRM, &vec, (struct sigvec *) 0);
  102.   if (!Debug) alarm(TIMEOUT);
  103.   umask(022);
  104.   if (!Debug) rtprio(0, 127);
  105.   if (!getenv("HOME"))
  106.     putenv("HOME=/users/root");
  107.   if (!getenv("LOGNAME"))
  108.     putenv("LOGNAME=root");
  109.   if (!getenv("PATH"))
  110.     putenv("PATH=/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin");
  111.   if (!getenv("SHELL"))
  112.     putenv("SHELL=/bin/sh");
  113.   if (!getenv("TZ"))
  114.     putenv("TZ=MEZ-1MESZ");
  115.   timerproc();          /* Init times */
  116.   fixutmpfile();
  117. }
  118.  
  119. /*---------------------------------------------------------------------------*/
  120.  
  121. void iostop()
  122. {
  123.   register struct iface *ifp;
  124.  
  125.   if (local_kbd) {
  126.     ioctl(0, TCSETA, &prev_termio);
  127.     printf("\033&s0A");   /* disable XmitFnctn */
  128.   }
  129.   for (ifp = Ifaces; ifp; ifp = ifp->next)
  130.     if (ifp->stop) (*ifp->stop)(ifp);
  131. }
  132.  
  133. /*---------------------------------------------------------------------------*/
  134.  
  135. int  system(cmdline)
  136. const char *cmdline;
  137. {
  138.  
  139.   int  i, pid, status;
  140.   long  oldmask;
  141.  
  142.   if (!cmdline) return 1;
  143.   fflush(stdout);
  144.   switch (pid = fork()) {
  145.   case -1:
  146.     return (-1);
  147.   case 0:
  148.     for (i = 3; i < _NFILE; i++) close(i);
  149.     execl("/bin/sh", "sh", "-c", cmdline, (char *) 0);
  150.     exit(1);
  151.   default:
  152.     oldmask = sigsetmask(-1);
  153.     while (wait(&status) != pid) ;
  154.     sigsetmask(oldmask);
  155.     return status;
  156.   }
  157. }
  158.  
  159. /*---------------------------------------------------------------------------*/
  160.  
  161. int  _system(cmdline)
  162. char  *cmdline;
  163. {
  164.   return system(cmdline);
  165. }
  166.  
  167. /*---------------------------------------------------------------------------*/
  168.  
  169. int  doshell(argc, argv, p)
  170. int  argc;
  171. char  *argv[];
  172. void *p;
  173. {
  174.   char  buf[2048];
  175.  
  176.   *buf = '\0';
  177.   while (--argc > 0) {
  178.     if (*buf) strcat(buf, " ");
  179.     strcat(buf, *++argv);
  180.   }
  181.   return system(buf);
  182. }
  183.  
  184. /*---------------------------------------------------------------------------*/
  185.  
  186. #define setmask(mask, fd) ((mask)[(fd)>>5] |=  (1 << ((fd) & 31)))
  187. #define clrmask(mask, fd) ((mask)[(fd)>>5] &= ~(1 << ((fd) & 31)))
  188. #define maskset(mask, fd) ((mask)[(fd)>>5] &   (1 << ((fd) & 31)))
  189.  
  190. /*---------------------------------------------------------------------------*/
  191.  
  192. void on_read(fd, fnc, arg)
  193. int  fd;
  194. void (*fnc) __ARGS((void *));
  195. void *arg;
  196. {
  197.   readfnc[fd] = fnc;
  198.   readarg[fd] = arg;
  199.   setmask(chkread, fd);
  200.   clrmask(actread, fd);
  201.   nfds = -1;
  202. }
  203.  
  204. /*---------------------------------------------------------------------------*/
  205.  
  206. void off_read(fd)
  207. int  fd;
  208. {
  209.   readfnc[fd] = 0;
  210.   readarg[fd] = 0;
  211.   clrmask(chkread, fd);
  212.   clrmask(actread, fd);
  213.   nfds = -1;
  214. }
  215.  
  216. /*---------------------------------------------------------------------------*/
  217.  
  218. void on_write(fd, fnc, arg)
  219. int  fd;
  220. void (*fnc) __ARGS((void *));
  221. void *arg;
  222. {
  223.   writefnc[fd] = fnc;
  224.   writearg[fd] = arg;
  225.   setmask(chkwrite, fd);
  226.   clrmask(actwrite, fd);
  227.   nfds = -1;
  228. }
  229.  
  230. /*---------------------------------------------------------------------------*/
  231.  
  232. void off_write(fd)
  233. int  fd;
  234. {
  235.   writefnc[fd] = 0;
  236.   writearg[fd] = 0;
  237.   clrmask(chkwrite, fd);
  238.   clrmask(actwrite, fd);
  239.   nfds = -1;
  240. }
  241.  
  242. /*---------------------------------------------------------------------------*/
  243.  
  244. void on_excp(fd, fnc, arg)
  245. int  fd;
  246. void (*fnc) __ARGS((void *));
  247. void *arg;
  248. {
  249.   excpfnc[fd] = fnc;
  250.   excparg[fd] = arg;
  251.   setmask(chkexcp, fd);
  252.   clrmask(actexcp, fd);
  253.   nfds = -1;
  254. }
  255.  
  256. /*---------------------------------------------------------------------------*/
  257.  
  258. void off_excp(fd)
  259. int  fd;
  260. {
  261.   excpfnc[fd] = 0;
  262.   excparg[fd] = 0;
  263.   clrmask(chkexcp, fd);
  264.   clrmask(actexcp, fd);
  265.   nfds = -1;
  266. }
  267.  
  268. /*---------------------------------------------------------------------------*/
  269.  
  270. static void check_files_changed()
  271. {
  272.  
  273.   int  changed = 0;
  274.   static long  nexttime, net_time, rc_time;
  275.   struct stat statbuf;
  276.  
  277.   if (Debug || nexttime > secclock()) return;
  278.   nexttime = secclock() + 600;
  279.  
  280.   if (stat("/tcp/net", &statbuf)) return;
  281.   if (!net_time) net_time = statbuf.st_mtime;
  282.   if (net_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  283.     changed = 1;
  284.  
  285.   if (stat(Startup, &statbuf)) return;
  286.   if (!rc_time) rc_time = statbuf.st_mtime;
  287.   if (rc_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  288.     changed = 1;
  289.  
  290.   if (changed) doexit(0, (char **) 0, (void *) 0);
  291. }
  292.  
  293. /*---------------------------------------------------------------------------*/
  294.  
  295. void eihalt()
  296. {
  297.  
  298.   int  n;
  299.   int  status;
  300.   int32 nte;
  301.   register unsigned int  i;
  302.   struct timeval timeout;
  303.  
  304.   check_files_changed();
  305.   wait3(&status, WNOHANG, (int *) 0);
  306.   if (!Debug) alarm(TIMEOUT);
  307.   if (nfds < 0) {
  308.     if (i = chkread[1] | chkwrite[1] | chkexcp[1])
  309.       nfds = 32;
  310.     else {
  311.       i = chkread[0] | chkwrite[0] | chkexcp[0];
  312.       nfds = 0;
  313.     }
  314.     for (n = 16; n; n >>= 1)
  315.       if (i & ((-1) << n)) {
  316.     nfds += n;
  317.     i >>= n;
  318.       }
  319.     if (i) nfds++;
  320.   }
  321.   actread [0] = chkread [0];
  322.   actread [1] = chkread [1];
  323.   actwrite[0] = chkwrite[0];
  324.   actwrite[1] = chkwrite[1];
  325.   actexcp [0] = chkexcp [0];
  326.   actexcp [1] = chkexcp [1];
  327.   timeout.tv_sec = 0;
  328.   if (Hopper)
  329.     timeout.tv_usec = 0;
  330.   else {
  331.     nte = next_timer_event();
  332.     if (nte > 999) nte = 999;
  333.     timeout.tv_usec = 1000 * nte;
  334.   }
  335.   if (select(nfds, actread, actwrite, actexcp, &timeout) < 1) {
  336.     actread [0] = actread [1] = 0;
  337.     actwrite[0] = actwrite[1] = 0;
  338.     actexcp [0] = actexcp [1] = 0;
  339.   } else
  340.     for (n = nfds - 1; n >= 0; n--) {
  341.       if (readfnc [n] && maskset(actread , n)) (*readfnc [n])(readarg [n]);
  342.       if (writefnc[n] && maskset(actwrite, n)) (*writefnc[n])(writearg[n]);
  343.       if (excpfnc [n] && maskset(actexcp , n)) (*excpfnc [n])(excparg [n]);
  344.     }
  345. }
  346.  
  347.